<?php

class DefaultController extends Controller
{
	public $sendActivationMail=true;
	public $loginNotActiv=false;
	public $activeAfterRegister=false;
	public $autoLogin=true;
	public $registrationUrl = array("/user/registration");
	public $recoveryUrl = array("/user/recovery/recovery");
	public $loginUrl = array("/user/login");
	public $logoutUrl = array("/user/logout");
	public $profileUrl = array("/user/profile");
	public $returnUrl = array("/user/profile");
	public $returnLogoutUrl = array("/user/login");
	
	public function actionIndex()
	{
		$this->layout='webroot.themes.grayscale.views.layouts.main';
		Yii::app()->clientScript->scriptMap['bootstrap.min.js'] = false;
		Yii::app()->clientScript->scriptMap['bootstrap-noconflict.js'] = false;
		Yii::app()->clientScript->scriptMap['*.css'] = false;
		$model = new RegistrationForm;
		$login=new UserLogin;
		$profile=new Profile;
		$profile->regMode = true;
		
		if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
			{
				echo UActiveForm::validate(array($model,$profile));
				Yii::app()->end();
			}
			
		    if (Yii::app()->user->id) {
		    	$this->redirect($this->profileUrl);
		    } else {
		    	if(isset($_POST['RegistrationForm'])) {
					$model->attributes=$_POST['RegistrationForm'];
					$profile->attributes=((isset($_POST['Profile'])?$_POST['Profile']:array()));
					if($model->validate()&&$profile->validate())
					{
						$soucePassword = $model->password;
						$model->activkey=UserModule::encrypting(microtime().$model->password);
						$model->password=UserModule::encrypting($model->password);
						$model->verifyPassword=UserModule::encrypting($model->verifyPassword);
						$model->superuser=0;
						$model->status=0;
						
						if ($model->save()) {
							$profile->user_id=$model->id;
							$profile->save();
							if ($this->sendActivationMail) {
								$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $model->activkey, "email" => $model->email));
								// UserModule::sendMail($model->email,UserModule::t("You registered from {site_name}",array('{site_name}'=>Yii::app()->name)),UserModule::t("Please activate you account go to {activation_url}",array('{activation_url}'=>$activation_url)));
								$this->sendNotification($model->email, "Activation from ".Yii::app()->name ,"You registered from ".Yii::app()->name." kami akan memproses permintaan anda dan melihat data anda sebelum kami approve");
								$this->sendNotification("biobienet@gmail.com", "Permintaan Member" ,"Permintaan aktivasi dari member : ". $model->username);
							}
							
							if (($this->loginNotActiv||($this->activeAfterRegister&&$this->sendActivationMail==false))&&$this->autoLogin) {
									$identity=new UserIdentity($model->username,$soucePassword);
									$identity->authenticate();
									Yii::app()->user->login($identity,0);
									$this->redirect(Yii::app()->controller->module->returnUrl);
							} else {
								if (!$this->activeAfterRegister&&!$this->sendActivationMail) {
									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Contact Admin to activate your account."));
								} elseif($this->activeAfterRegister&&Yii::app()->controller->module->sendActivationMail==false) {
									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please {{login}}.",array('{{login}}'=>CHtml::link(UserModule::t('Login'),Yii::app()->controller->module->loginUrl))));
								} elseif($this->loginNotActiv) {
									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email or login."));
								} else {
									Yii::app()->user->setFlash('registration',UserModule::t("Thank you for your registration. Please check your email."));
								}
								
								$this->redirect(Yii::app()->createUrl('/home/default/msReg'));
							}
						}
					} else $profile->validate();
				}
			    // $this->render('index',array('model'=>$model,'profile'=>$profile));
		    }
		
		$this->render('index',array('model'=>$model,'profile'=>$profile,'login'=>$login));
	}
	
	public function actionMsReg(){
		$this->layout='webroot.themes.barnie.views.layouts.home';
		$this->render('msReg');
	}
	
	public function actions()
	{
		return array(
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0x669933,
			),
		);
	}
	
	protected function sendNotification($model, $subject, $view) {
      $message = new YiiMailMessage;
      // $message->view = $view;
      $message->setBody($view);
      $message->subject = $subject;
      $message->addTo($model);
      $message->from = "biobienet@gmail.com";
      Yii::app()->mail->send($message);
   }
	
	 public function actionActivationMail($id){
		$model = User::model()->findByPk($id); 
		$activation_url = $this->createAbsoluteUrl('/user/activation/activation',array("activkey" => $model->activkey, "email" => $model->email));
		$this->sendNotification($model->email, "Activation from ".Yii::app()->name ,"Akun anda akan aktif silahkan klik $activation_url");
		$this->render($this->redirect(Yii::app()->createUrl('/user/admin?activeMail=1')));
   }
}